<?php


/**
 * 商品竞拍
 * Enter description here ...
 * @author Admin
 *
 */
class AuctionAction extends CommonAction{
	
	
	
	/**
	 * 即将竞拍
	 * Enter description here ...
	 * @param unknown_type $savingprice
	 */
    public function _switchFuture($savingprice){
		switch ($savingprice) {
		    case 1:
		        $where=array('between','0,100');
		        break;
		    case 2:
		        $where=array('between','100,500');
		        break;
			case 3:
				$where=array('between','500,1000');
				break;
			case 4:
				$where=array('between','1000,2000');
				break;
			case 5:
				$where=array('gt',2000);
				break;
			default:
				break;
		}
		
		return $where;
    }
    
    
    
    /**
     * 竞拍历史
     * Enter description here ...
     * @param unknown_type $price
     */
    public function _switchHistory($price){
		switch ($price) {
		    case 1:
		        $where=array('between','0,1');
		        break;
		    case 2:
		        $where=array('between','1,5');
		        break;
			case 3:
				$where=array('between','5,10');
				break;
			case 4:
				$where=array('between','10,100');
				break;
			case 5:
				$where=array('gt',100);
				break;
			default:
				break;
		}
		
		return $where;
    }
	
	
    /**
     * 格式化JSON数据
     * Enter description here ...
     * @param unknown_type $auctiondata
     */
	 public function _format($auctiondata){
		if($auctiondata['state'] != 3){
			$config = C('SYSCONFIG');
			$auctiondata['ordertime'] = time() + $config['order_expired'] * 86400;   //订单时间  + 订单实效周期
		}

    	$data = array(
    		'price'=>$auctiondata['realprice'],
    		'endtime'=>date('Y-m-d H:i:s',$auctiondata['ordertime']),
    		'productname'=>$auctiondata['goodsname'],
    		'lastopplayer'=>$auctiondata['auctionuname']
    	);
    	
    	return $data;
    } 
    
    
	
    
    
	/**
	 * 正在进行的竞拍
	 * Enter description here ...
	 */
    public function auctionnow()    {
    	$data = Init_GP(array('brands','savingprice'));   //分类和保价
    	
    	$brands = GetNum($data['brands']);     //分类
    	$savingprice = GetNum($data['savingprice']);  //报价
    	
    	//Log::write("...检索正在进行中竞拍...分类:".$brands .'......保价:' .$savingprice , Log::DEBUG);
    	
    	$category = D('Category');
    	$auction = D('Auction');
    	
    	
    	$where['A.state'] = array('eq',1); //获取数据的条件
    	
    	if ($brands)
    		$where['G.cid'] = array('eq',$brands);
    	if ($savingprice)
    		$where['A.goodsprice'] = $this->_switchFuture($savingprice);
    	
		$count = $auction->countNum($where);    //计算总数
		
		$limit = $this->page($count,15,$data);
		$now = $auction->getFuture($where,$limit);   //分页查询
		
		
		 //层级结构的商品分类
		$categorydata=$category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")->where('state=1')->order('bpath')->select();   //获取启用状态的类型
		
		foreach($categorydata as $key=>$value){
			$categorydata[$key]['count']=count(explode('-',$value['bpath']));
		}
    	
		
		foreach ($now as $val){
			$tem[] = $val['id'];
		}
		
		$ids = implode(',', $tem);
		
		$this->assign('ids',$ids);
    	$this->assign('now',$now);
    	
		$this->assign('brands',$brands);
		$this->assign('savingprice',$savingprice);
		$this->assign('categorydata',$categorydata);
    	
    	
    	$this->display();
    }
    
    
    
    
    
    /**
     * 即将进行的竞拍
     * Enter description here ...
     */
	public function futurelist()   {
    	$data = Init_GP(array('brands','savingprice'));   //分类和保价
    	
    	$auction = D('Auction');
    	$category = D('Category');
    	
    	$brands = GetNum($data['brands']);     //分类
    	$savingprice = GetNum($data['savingprice']);  //报价
    	
    	
    	if ($brands)
    		$where['G.cid'] = array('eq',$brands);
    	if ($savingprice)
    		$where['A.goodsprice'] = $this->_switchFuture($savingprice);
    	
    	$where['A.state'] = array('eq',0);     //状态0
    	    	
		$count = $auction->countNum($where);    //计算总数
		
		$limit = $this->page($count,15,$data);
		$future = $auction->getFuture($where,$limit);
		
		
		
		//层级结构的商品分类
		$categorydata=$category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")->where('state=1')->order('bpath')->select();   //获取启用状态的类型
		
		foreach($categorydata as $key=>$value){
			$categorydata[$key]['count']=count(explode('-',$value['bpath']));
		}
    	
		
		$this->assign('brands',$brands);
		$this->assign('savingprice',$savingprice);
		
    	$this->assign('future',$future);
    	
    	$this->assign('categorydata',$categorydata);
    	
    	$this->display();
    }
    
    
    
    /**
     * 历史竞拍
     * Enter description here ...
     */
    public function historylist()    {
    	$data = Init_GP(array('brands','price'));   //分类和成交价
    	$auction = D('Auction');
    	$category = D('Category');
    	
    	$brands = GetNum($data['brands']); 
    	$price = GetNum($data['price']);
    	
    	if ($brands)
    		$where['G.cid'] = array('eq',$brands);
    	if ($price)
    		$where['A.realprice'] = $this->_switchHistory($price);
    	
    	$where['A.state'] = array('egt',2); //竞拍完成以上状态
    	    	
		$count = $auction->countNum($where);    //计算总数
		$limit = $this->page($count,15,$data);    //分页
		$history = $auction->getHistory($where,$limit);


    	//层级结构的商品分类
		$categorydata=$category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")->where('state=1')->order('bpath')->select();   //获取启用状态的类型
		
		foreach($categorydata as $key=>$value){
			$categorydata[$key]['count']=count(explode('-',$value['bpath']));
		}
		
		$this->assign('brands',$brands);
		$this->assign('savingprice',$savingprice);
		
    	$this->assign('history',$history);
    	
    	$this->assign('categorydata',$categorydata);
    	
    	$this->display();
    }
    
    
    
    
    
    
    
    
    /**
     * 获取竞拍提示
     * 
     * Enter description here ...
     */
    public function gamesTips(){
    	$data = Init_GP(array('id'));
    	$id = GetNum($data['id']);
    	$auction = D('Auction');
    	$auctiondata = $auction->find($id);
    	if (empty($auctiondata)){
    		$istip = 0; 
    	}else {
    		if ($auctiondata['state'] < 2){  //未开始或进行中
    			$istip = 0;
    		}else {  //已结束或生成了订单
    			$istip = 1;
				$userdata = $_SESSION['user'];
				if ($auctiondata['uid'] == $userdata['id']){   //商品竞标人就是当前用户
					$tipstate = 0;
					$tipdata = $this->_format($auctiondata);
				}else {
					$auction_log = D('Auction_log');
					$auction_logmap = array(
						'aid'=>array('eq',$id),
						'uid'=>array('eq',$userdata['id'])
					);
					$auction_logdata = $auction_log->where($auction_logmap)->find();  //竞拍历史
					if (empty($auction_logdata)){  //不存在竞拍
						$tipstate = 3;
						$tipdata = $this->_format($auctiondata);
					}else {
						$tipstate = 1;
						$tipdata = $this->_format($auctiondata);
						$countdata = $auction_log->where($auction_logmap)->field('sum(beat) as beatsum')->find();  //统计竞拍所用实体币数
						
						$config = C('SYSCONFIG');
						$count = $auctiondata['goodsprice'] - $countdata['beatsum'] / $config['rmb_beat']; //换算差价金额
						
						$tipdata['saleprice'] = $count;
					}
				}
    		}
    	}
    	
    	/*$tipdata 竞拍数据
    	 *$tipstate 提示状态 哪种提示   0自己获取结束提示       1提示保价     3提示正常结束                  2第2名         4返点        5恭喜他人  
    	 *$istip 是否提示 0不提示 1 提示
    	 * [3] 保留无用
    	 * */
    	$json = array($tipdata,$tipstate,$istip,0);
    	
    	echo json_encode($json);
    } 
    
   
    
    
    
    
    
    
    /**
     * 竞拍详细页面
     * Enter description here ...
     */
    public function detail(){
    	$data = Init_GP(array('id'));   //竞拍ID
    	$id = GetNum($data['id']);
    	$auction = D('Auction');
    	
    	$one = $auction->getOne($id);   //竞拍信息
    	
    	$where['A.state'] = array('eq',1); //获取数据的条件
    	$where['A.id'] = array('neq',$one['id']); 
    	$now = $auction->getdata($where,3);    //获取正在竞拍的所有信息（不包括当前竞拍）
    	
    	if ($one['state'] >= 2){   //竞拍已经结束或生成订单等情况
	    	$this->assign('ago',$ago);
	    	
	    	
	    	/*
	    	$sunpic = D('Sunpic');
	    	$sunpicmap['S.aid'] = array('eq',$one['id']);
			$sunpicmap['S.state'] = array('eq',1);
	    	$show = $sunpic->getdata($sunpicmap); 
	    	
	    	$this->assign('show',$show);
	    	*/
	    	
	    	
	    	$user = D('User');         //最后竞拍人信息
	    	if (!empty($one['uid'])){
	    		$userdata = $user->find($one['uid']);
	    	}
	    	
	    	$auction_log = D('Auction_log');  //竞拍日志
	    	$auction_logmap['aid'] = array('eq',$one['id']);
	    	$auction_logdata = $auction_log->where($auction_logmap)->order('id desc')->limit(10)->findAll();
	    	
	    	
	    	$express = D('Express');      //物流
	    	$expressmap['aid'] = array('eq',$one['id']);
	    	$expressdata = $express->where($expressmap)->order('id desc')->findAll();
	    	
	    	
	    	$participate = D('Participate');   //竞拍参与人信息
	    	$participatedata = $participate->getUserData($one['id']);
	    	
	    	
	    	$this->assign('participatedata',$participatedata);
	    	$this->assign('auction_logdata',$auction_logdata);
	    	$this->assign('expressdata',$expressdata);
	    	$this->assign('userdata',$userdata);
	    	
	    	
    	}else{
    		/*
    		if (!empty($one['agoid'])){
		    	$ago = $auction->find($one['agoid']);
		    	$this->assign('ago',$ago);
		    	
		    	$sunpic = D('Sunpic');
		    	$sunpicmap['S.aid'] = array('eq',$ago['id']);
		    	$show = $sunpic->getdata($sunpicmap); 
		    	
		    	$this->assign('show',$show);
	    	}
	    	*/
    	}
    	
    	
    	$tem[] = $one['id'];
    	foreach ($now as $val){
			$tem[] = $val['id'];
		}
		$ids = implode(',', $tem);
		
		
    	$this->assign('one',$one);
    	
		if ($one['state'] >= 2){    //完成竞拍等状态
			
			$this->display('detail1');
		}else {
			
			$this->assign('now',$now);
			$this->assign('ids',$ids);
			
    		$this->display();
		}
		
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * 获取需要补差价数据
     * Enter description here ...
     */
    public function ajaxmycount(){
    	$data = Init_GP(array('id'));
    	$id = GetNum($data['id']);
    	
    	$auction_log = D('Auction_log');   //竞拍日志
    	
    	$user = $_SESSION['user'];
    	$map['aid']=array('eq',$id);
    	$map['uid']=array('eq',$user['id']);  //统计用户竞拍数据：实体币和虚拟币
    	$auction_logdata = $auction_log->where($map)->field('sum(beat) as self_point_a,sum(pointb) as self_point_b,count(*) as self_bidcount')->find();
    	
    	$config = C('SYSCONFIG');
		if(empty($auction_logdata['self_point_a']))
			$auction_logdata['self_point_a'] =0;
		if(empty($auction_logdata['self_point_b']))
			$auction_logdata['self_point_b'] =0;
			
    	$auction_logdata['self_price_point_a'] = $auction_logdata['self_point_a'] / $config['rmb_beat'];
    	
    	$json = json_encode($auction_logdata);
    	
    	echo $json;
    }
    
    
    
    
    
    
    /**
     * 保价购买
     * Enter description here ...
     */
    public function buynow(){
    	$data = Init_GP(array('id'));  //竞拍ID
    	$id = GetNum($data['id']);
    	$user = $_SESSION['user'];
    	if (empty($user)) {
    		$json = 2; //请登录
    	}else {
    		$insured = M('Insured');
    		$insuredmap = array(
    			'aid'=>array('eq',$id),
    			'uid'=>array('eq',$user['id'])
    		);
    		$tmp = $insured->where($insuredmap)->find();
    		if (empty($tmp)){
	    		$insureddata = array(
	    			'aid'=>$id,
	    			'uid'=>$user['id']
	    		);
	    		$info = $insured->add($insureddata);
	    		if ($info){
	    			$json = 0; //保价成功
	    		}else {
	    			$json = 1; //保价失败
	    		}
    		}else {
    			$json = 3; //已保价
    		}
    	}
    	echo $json;
    }
    
    
    
    
    
    
    /**
     * 下载竞拍数据
     * Enter description here ...
     */
    public function files(){
    	$data = Init_GP(array('id'));   //竞拍ID
    	$id = GetNum($data['id']);
    	
    	$auction = D('Auction');
    	$auctiondata = $auction->find($id);
    	
    	if ($auctiondata['state'] >=2 ){   //竞拍结束
    		$auction_log = D('Auction_log');
    		$auction_logmap['aid'] = array('eq',$auctiondata['id']);
    		$auction_logdata = $auction_log->where($auction_logmap)->findAll();
    		
    		foreach ($auction_logdata as $value){
    			$downdata[] = array(
    				'name'=>$value['name'],
    				'ip'=>formatip($value['ip']),
    				'address'=>$value['address'],
    				'addtime'=>$value['addtime'],
    				'bidprice'=>$value['bidprice']
    			);
    		}
    		
    		$keyname = array(
    			'name'=>'用户名',
    			'ip'=>'IP地址',
    			'address'=>'真实地址',
    			'addtime'=>'竞拍时间',
    			'bidprice'=>'竞拍价格'
    		);
    		
    		down_xls($downdata, $keyname,'竞拍记录_'.date("Y-m-d"));
    	}
    }
    
    
    
}
?>